package gov.fema.hazus.bdc.server.servlet;

import gov.fema.hazus.bdc.client.login.LoginService;
import gov.fema.hazus.bdc.client.services.ServerException;
import gov.fema.hazus.bdc.shared.entity.User;

import org.apache.log4j.Logger;

import com.googlecode.objectify.OQuery;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

public class LoginServlet implements LoginService {

	private final Logger logger = Logger.getLogger(LoginServlet.class);
	private DAO dao = new DAO();

    public LoginServlet()
    {
        
    }

	public User login(User user) throws ServerException {
		
		// get the stored user based on the incoming username
		Objectify ofy = ObjectifyService.begin();
		OQuery<User> q = ObjectifyService.createQuery(User.class);
		q.filter("username", user.getUsername());
		q.filter("password", user.getPassword());
		User fetched = ofy.prepare(q).asSingle();
		
		// if fetched is not null, we have logged in!
		if( fetched == null )
		{
			throw new ServerException("The username or password was incorrect. Please try again.");
		}
		return fetched;
	}

    
}
